<?php
namespace app\api\controller;
use app\api\controller\Apiuser;
use think\Db;
class User extends Apiuser{
	
   /**
    *  转发文章
    */
    public function copyArticle(){
        $data= $this->askData;
        if(!$this->val('copyArticle')){
            return $this->result= msback(150,'参数错误','');
        }
        $res=Db::name('article')->where(['id'=>$data['from_aid']])->find();
        //做转发处理
        $res['share_from_user_id']=$res['user_id'];
        $res['user_id']= $this->uid;
        $res['share_from_article_id']=$res['id'];unset($res['id']);unset($res['create_time']);unset($res['update_time']);
        $res['create_time']= time();
        $res['update_time']= time(); 
        $res1= Db::name('article')->insert($res);
        if($res1){
            Db::name('copy_history')->insert(['article_id'=>$data['from_aid'],'uid'=> $this->uid,'create_time'=> time()]);
            return $this->result=msback(200,'转发成功','');
        }else{
            return $this->result=msback(100,'转发失败','');
        }
    }
    
    /**
     * 获取搜索历史
     */
    public function getSearchHistory(){
         $res= Db::name('search_history')->where(['uid'=> $this->uid,'is_delete'=>0])
                 ->field('key')
                 ->order('create_time desc')
                 ->select();
         return $this->result=$res?msback(200,'请求成功',$res):msback(100,'无数据');
    }
   /**
    *  清空搜索历史
    */
    public function clearSearchHistory(){
       $res= Db::name('search_history')->where(['uid'=> $this->uid])->delete();
       return $this->result=$res?msback(200,'操作成功',''):msback(100,'操作失败','');
    }
    /**
     * 评论文章
     */
    public function comArticle(){
        $data= $this->askData;
        if(!$this->val('comArticle')){
            return $this->result= msback(150,'参数错误','');
        } 
        $insert=[
            'article_id'=>$data['article_id'],
            'user_id'=> $this->uid,
            'content'=>$data['content'],
            'create_time'=> time(),
            'update_time'=> time(),
        ];
        $res= Db::name('comment')->insert($insert);
        return $this->result=$res?msback(200,'评论成功'): msback(100,'评论失败');
    }
    /**
     * 删除评论
     */
    public function delComArticle(){
        $data= $this->askData;
        if(!$this->val('delComArticle')){
            return $this->result= msback(150,'参数错误','');
        }         
        $res= Db::name('comment')->where(['id'=>$data['comment_id'],'user_id'=> $this->uid])->delete();
        return $this->result=$res?msback(200,'操作成功'): msback(100,'操作失败');
    }
    /**
     * 点赞文章
     */
    public function addArticleLike(){
        $data= $this->askData;
        if(!$this->val('addArticleLike')){
            return $this->result= msback(150,'参数错误','');
        }
        if(Db::name('article_like')->where(['article_id'=>$data['article_id'],'user_id'=> $this->uid])->value('id')){
            return $this->result= msback(100,'请勿重复点赞');
        }
        $insert=[
            'article_id'=>$data['article_id'],
            'user_id'=> $this->uid,
            'create_time'=> time(),
            'update_time'=> time(),
        ];
        $res= Db::name('article_like')->insert($insert);
        return $this->result=$res?msback(200,'操作成功'): msback(100,'操作失败');
    }
   /**
    *  取消点赞
    */
    public function concelArticleLike(){
        $data= $this->askData;
        if(!$this->val('concelArticleLike')){
            return $this->result= msback(150,'参数错误','');
        }    
        $res= Db::name('article_like')->where(['article_id'=>$data['article_id'],'user_id'=> $this->uid])->delete();
        return $this->result=$res?msback(200,'操作成功'): msback(100,'操作失败');        
    }
   /**
    * 收藏文章
    */
    public function addArticleCatch(){
        $data= $this->askData;
        if(!$this->val('addArticleCatch')){
            return $this->result= msback(150,'参数错误','');
        }
        if(Db::name('article_catch')->where(['article_id'=>$data['article_id'],'user_id'=> $this->uid])->value('id')){
            return $this->result= msback(100,'请勿重复收藏');
        }        
        $insert=[
            'article_id'=>$data['article_id'],
            'user_id'=> $this->uid,
            'create_time'=> time(),
        ];
        $res= Db::name('article_catch')->insert($insert);
        return $this->result=$res?msback(200,'操作成功'): msback(100,'操作失败');
    }
    /**
     * 取消收藏
     */
    public function concelArticleCatch(){
        $data= $this->askData;
        if(!$this->val('concelArticleCatch')){
            return $this->result= msback(150,'参数错误','');
        }    
        $res= Db::name('article_catch')->where(['article_id'=>$data['article_id'],'user_id'=> $this->uid])->delete();
        return $this->result=$res?msback(200,'操作成功'): msback(100,'操作失败');           
    }
   /**
    * 获取我的基本信息
    */
    public function getUserInfo(){
        $host= $this->request->domain();
        $res= Db::name('user u')->where(['u.id'=> $this->uid])
                ->join('picture p','p.id = u.headimg')
                ->field('concat("'.$host.'",p.url) as headimgurl,u.id as uid,u.nickname,u.username')
                ->find();
        return $this->result=$res?msback(200,'请求成功',$res):msback(100,'无数据');
    }
    /**
     * 获取我的详细信息
     */
    public function getUserDetail(){
        $host= $this->request->domain();
        $res= Db::name('user u')->where(['u.id'=> $this->uid])
                ->join('picture p','p.id = u.headimg')
                ->field('concat("'.$host.'",p.url) as headimgurl,u.id as uid,u.nickname,u.username,u.sex,u.birthday,u.mobile')
                ->find();
        return $this->result=$res?msback(200,'请求成功',$res):msback(100,'无数据');        
    }
    /**
     * 更新用户信息
     */
    public function changeUserDetail(){
        $data= $this->askData;
            if(!$this->val('changeUserDetail')){
            return $this->result= msback(150,'参数错误','');
        }    
        unset($data['token']);
        if(isset($data['nickname'])){
            $is_ext= Db::name('user')->where(['nickname'=>$data['nickname']])->value('id');
            if($is_ext){
                return $this->result= msback(151,'昵称重复，请更换其他昵称','');
            }            
        }
        $res= Db::name('user')->where(['id'=> $this->uid])->update($data);
        return $this->result=$res?msback(200,'操作成功'): msback(100,'操作失败');        
    }
    /**
     * 修改昵称
     */
    public function changeNickname(){
        $data= $this->askData;
            if(!$this->val('changeNickname')){
            return $this->result= msback(150,'参数错误','');
        }  
        $is_ext= Db::name('user')->where(['nickname'=>$data['nickname']])->value('id');
        if($is_ext){
            return $this->result= msback(151,'昵称重复，请更换其他昵称','');
        }
        $res= Db::name('user')->where(['id'=> $this->uid])->update(['nickname'=>$data['nickname']]);
        return $this->result=$res?msback(200,'操作成功'): msback(100,'操作失败');
    }
   /**
    * 获取我的文章信息
    */
    public function getMyArticle(){
        if(!$this->val('getMyArticle')){
            return $this->result= msback(150,'参数错误','');
        }     
        $res= Db::name('article a')
                ->join('xy_user u','u.id = a.user_id')
                ->join('xy_category c','c.id = a.category_id')
                ->where(['a.user_id'=> $this->uid])
                ->field('a.title,a.create_time,u.nickname,c.name,a.view_num,a.status,a.id as article_id,a.category_id as cat_id,a.video_list')
                ->order('a.create_time desc,a.view_num')
//                ->limit(10)
                ->select();
        return  $this->result=$res?msback(200,'有数据',$res):msback(100,'无数据');           
    }
   /**
    * 更新用户名或者密码
    */
    public function changeUserName(){
        $data= $this->askData;
        if(!$this->val('changeUserName')){
            return $this->result= msback(150,'参数错误','');
        }
        $res0= Db::name('user')->where(['token'=>$data['token'],'username'=>$data['username']])->value('id');
        if(!$res0){
            return $this->result= msback(151,'用户鉴权失败，请输入正确的用户名','');
        }
        $res1=\app\api\model\Api::checkCode($data['newusername'],$data['code']);
        if(!$res1){
            return $this->result= msback(152,'验证码错误','');
        }        
        $res2= Db::name('user')->where(['username'=>$data['newusername']])->value('id');
        if($res2){
            return $this->result= msback(153,'此手机已被绑定请更换手机号重试','');
        }
        $res=Db::name('user')
                ->where(['id'=> $this->uid])
                ->update(['username'=>$data['newusername']]);
        return $this->result=$res?msback(200,'操作成功'): msback(100,'操作失败');                
    } 
    /**
     * 获取我的评论
     */
    public function getMyComment(){
        $data= $this->askData;
        if(!$this->val('getMyComment')){
            return $this->result= msback(150,'参数错误','');
        }
        $res= Db::name('comment c')->where(['c.user_id'=>$this->uid])
              ->join('xy_article a',' a.id = c.article_id')
              ->field('a.title,c.content,c.create_time,c.id as comment_id,a.is_delete,a.id as article_id,a.video_list')
              ->order('c.create_time desc')
              ->limit($data['offset'],$data['page'])
              ->select();
        return  $this->result=$res?msback(200,'有数据',$res):msback(100,'无数据');  
    }
    /**
     * 获取我的点赞
     */
    public function getMylike(){
        $data= $this->askData;
        if(!$this->val('getMylike')){
            return $this->result= msback(150,'参数错误','');
        }
        $host= $this->request->domain();
        $res= Db::name('article a')
                ->join('xy_article_like l','l.article_id = a.id')
                ->join('xy_user u','a.user_id = u.id')
                ->join('xy_article_catch ca','ca.article_id = a.id','left')
                ->join('xy_copy_history h','h.article_id = a.id','left')
                ->join('xy_picture p','p.id = u.headimg','left')
                ->field('concat("'.$host.'",p.url) as headimgurl,if(ca.id,1,0) as is_catch,a.video_list,a.is_delete,l.create_time as like_time,a.id as article_id,a.category_id as cat_id,a.title,a.view_num,count(l.id) as likenum,count(h.id) as copynum,a.create_time,u.nickname,u.id as uid')
                ->where(['l.user_id'=> $this->uid])
                ->group('l.id')
                ->limit($data['offset'],$data['page'])
                ->select();
        return  $this->result=$res?msback(200,'有数据',$res):msback(100,'无数据');  
    }
    /**
     *获取我的收藏
     */
    public function getMyCatch(){
        $data= $this->askData;
        if(!$this->val('getMyCatch')){
            return $this->result= msback(150,'参数错误','');
        }
        $host= $this->request->domain();
        $res= Db::name('article_catch ca')
                ->join('xy_article a','ca.article_id = a.id')
                ->join('xy_article_like l','l.article_id = a.id','left')
                ->join('xy_user u','a.user_id = u.id','left')
                ->join('xy_article_like al','al.user_id = ca.user_id and al.article_id = ca.article_id','left')
                ->join('xy_copy_history h','h.article_id = a.id','left')
                ->join('xy_picture p','p.id = u.headimg','left')
                ->field('concat("'.$host.'",p.url) as headimgurl,if(ca.id,1,0) as is_catch,a.video_list,if(al.id,1,0) as is_like,a.is_delete,a.id as article_id,a.title,a.view_num,ca.create_time as catch_time,count(l.id) as likenum,count(h.id) as copynum,a.category_id as cat_id,a.create_time,u.nickname,u.id as uid')
                ->where(['ca.user_id'=> $this->uid])
                ->group('ca.id')
                ->limit($data['offset'],$data['page'])
                ->select();
        return  $this->result=$res?msback(200,'有数据',$res):msback(100,'无数据');  
        
    }
    /**
     * 问题反馈
     */
    public function QueFeedback(){
        $data= $this->askData;
        if(!$this->val('QueFeedback')){
            return $this->result= msback(150,'参数错误','');
        }      
        $insert=[
            'user_id'=> $this->uid,
            'content'=>$data['content'],
            'create_time'=> time(),
        ];
        $res= Db::name('feedback')->insert($insert);
       return  $this->result=$res?msback(200,'提交成功'):msback(100,'提交失败'); 
    }
   /**
    * 上传文章
    */
    public function updateArticle(){
        $data= $this->askData;
        if(!$this->val('updateArticle')){
            return $this->result= msback(150,'参数错误','');
        }     
        $insertdata=[
            'keywords'=> trim($data['keywords'],','),
            'title'=>$data['title'],
            'category_id'=>$data['category_id'],
            'content'=>$data['content'],
            'user_id'=> $this->uid,
            'create_time'=> time(),
            'update_time'=> time(),
            'is_hot'=>0,
        ];
        if(!empty($data['pic_list'])){
            $insertdata['pic_list']=trim($data['pic_list'],',');
        }
        if(!empty($data['video_list'])){
            $insertdata['video_list']=$data['video_list'];
        }
        $res= Db::name('article')->insert($insertdata);
        return $this->result=$res?msback(200,'操作成功'):msback(100,'操作失败');
    }   
    /**
    * 上传用户头像
    */
    public function updateUserImg(){
        $host= $this->request->domain();
        if(empty($_FILES['file'])){
            return $this->result= msback(100,'上传文件不存在或者名称错误');
        }
        $upload=new \app\common\controller\Upload();
        $fileinfo=$upload->Fupload();
        if($fileinfo){
           $back=[
               'id'=>$fileinfo['id'],
               'url'=> $host.$fileinfo['url'],
               'md5'=>$fileinfo['md5'],
           ]; 
           return $this->result= msback(200,'上传成功',$back);
        }
        return $this->result= msback(100,'上传失败');
    }
    /**
     *  获取我的文章列表搜索
     */
    public function searchUserArticle(){
        $data= $this->askData;
        if(!$this->val('searchUserArticle')){
            return $this->result= msback(150,'参数错误','');
        }  
        $where=[
            'a.user_id'=> $this->uid,  
        ];        
        $host= $this->request->domain();
        empty($data['start_time']) or $where['a.update_time']=['>=', strtotime($data['start_time'].' 00:00:00')];
        empty($data['end_time']) or $where['a.update_time']=['<=', strtotime($data['end_time'].' 23:59:59')];
        if(!empty($data['start_time']) && !empty($data['end_time'])){
            $where['a.update_time']=['between',[strtotime($data['start_time'].' 00:00:00'),strtotime($data['end_time'].' 23:59:59')]];
        }
        $where1='';
        $keywords=$data['keywords'];
        empty($data['keywords']) or $where1="(a.title like '%$keywords%' or a.content like '%$keywords%' or a.keywords like '%$keywords%')";
        $res= Db::name('article a')
            ->join('xy_user u','u.id = a.user_id','left')
            ->join('xy_article_like l','l.article_id = a.id','left')
            ->join('xy_copy_history h','h.article_id = a.id','left') 
            ->join('xy_article_catch ca','ca.article_id = a.id','left')
            ->join('xy_picture p','p.id = u.headimg','left')
            ->join('xy_category c','c.id = a.category_id','left')
            ->field('concat("'.$host.'",p.url) as headimgurl,count(l.id) as likenum,count(h.id) as copynum,a.video_list,a.other_share_num,a.id,a.view_num,u.nickname,a.is_hot,a.title,a.category_id as cat_id,a.status,a.pic_list,a.create_time,a.update_time,c.name,a.share_from_user_id,a.share_from_article_id')
            ->where($where)
            ->where($where1)    
            ->group('a.id')                                                                                                                                                                             
            ->order('a.create_time desc,a.view_num desc')
            ->limit($data['offset'],$data['page'])->select();
        foreach($res as $k=>$v){
              #文章列表
              if($v['pic_list']){
                  $host= $this->request->domain();
                  $plist= Db::name('picture')->where("id in (".$v['pic_list'].")")->field("concat('{$host}',url) as url")->select();
                  $res[$k]['pic_list']=$plist;
              }             
              #看下是否登录过
              $res[$k]['is_like']=0;
              $res[$k]['is_catch']=0;
              $is_res= Db::name('article_like')->where(['article_id'=>$v['id'],'user_id'=> $this->uid])->value('id');
              if($is_res){
                  $res[$k]['is_like']=1;
              }
              $is_catch= Db::name('article_catch')->where(['article_id'=>$v['id'],'user_id'=> $this->uid])->value('id');
              if($is_catch){
                  $res[$k]['is_catch']=1;
              }            
        }
        return $this->result=$res?msback(200,'请求成功',$res):msback(100,'无数据');
        
    }                             




}